package controller.users;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Query;
import org.hibernate.Session;

import utils.HibernateUtils;

public class LoginAction extends Action {

	private static Session s = null;
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) throws Exception {
			String resultat = null;
			String name = ((LoginForm) form).getName();
			String pass = ((LoginForm) form).getPass();
			
			s = HibernateUtils.getSession();
			
			Query q = s.createQuery("FROM User WHERE name=:name AND pass=:pass");
			q.setParameter("name", name);
			q.setParameter("pass", pass);
			
			if (q.uniqueResult()!= null)
			{
				resultat = "succes";
			}
			else
			{
				resultat = "echec";
			}
			
		return mapping.findForward(resultat);
	}
	
}
